<div class="layui-card">
    <div class="layui-card-header">
        <h2 class="header-title">称号类型管理</h2>
        <span class="layui-breadcrumb pull-right">
          <a href="#!console">首页</a>
          <a><cite>称号类型管理</cite></a>
        </span>
    </div>
    <div class="layui-card-body">
        <div class="layui-form toolbar">
            <input id="titleType-search-value" class="layui-input search-input" type="text" placeholder="输入称号类型名称"/>&emsp;
            <button id="titleType-btn-search" class="layui-btn icon-btn"><i class="layui-icon">&#xe615;</i>搜索</button>
            <button id="titleType-btn-add" class="layui-btn icon-btn"><i class="layui-icon">&#xe654;</i>添加称号类型</button>
        </div>

        <table class="layui-table" id="titleType-table" lay-filter="titleType-table"></table>
    </div>
</div>


<!-- 表格操作列 -->
<script type="text/html" id="titleType-table-bar">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>


<script>
    layui.use(['form', 'index','table', 'util', 'config', 'admin'], function () {
        var form = layui.form;
        var table = layui.table;
        var config = layui.config;
        var layer = layui.layer;
        var util = layui.util;
        var admin = layui.admin;

        // 渲染表格
        table.render({
            elem: '#titleType-table',
            // url: config.base_server + 'user_query.json',
            url: config.base_server + 'api-user/titleType/getSelectTitleTypes'+'?access_token='+config.getToken().access_token,
            method: 'post',
            where: {
                 // access_token: config.getToken().access_token
            },
            page: true,
            cols: [[
                {type: 'numbers'},
                {field: 'tpname', sort: true, title: '称号类型名称'},
                {field: 'tpCode', sort: true, title: '称号类型CODE'},

                {
                    sort: true, templet: function (d) {
                        return util.toDateString(d.cretim);
                    }, title: '创建时间'
                },
                {
                    sort: true, templet: function (d) {
                        return util.toDateString(d.updtim);
                    }, title: '修改时间'
                },
                {align: 'center', toolbar: '#titleType-table-bar', title: '操作',width: 150}
            ]]
        });

        // 工具条点击事件
        table.on('tool(titleType-table)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') { //修改
                showEditModel(data);
            } else if (obj.event === 'del') { //删除
                doDelete(obj);
            }
        });


        // 搜索按钮点击事件
        $('#titleType-btn-search').click(function () {
            var value = $('#titleType-search-value').val();
            table.reload('titleType-table', {where: {searchKey: value}});
        });

        // 添加按钮点击事件
        $('#titleType-btn-add').click(function () {
            showEditModel();
        });


        //显示修改表单弹窗
        var showEditModel = function (data) {
            // debugger;
            admin.putTempData('titleType', data);
            var title = data ? '修改称号类型':'添加称号类型';
            admin.popupCenter({
                title: title,
                path: 'pages/system/titleType_form.html',
                finish: function () {
                    table.reload('titleType-table', {});
                }
            });
        };

        // 删除
        var doDelete = function (obj) {
            layer.confirm('确定要删除吗？', function (i) {
                layer.close(i);
                layer.load(2);
                admin.req('api-user/titleType/delTitleType/' + obj.data.tttpid, {}, function (data) {
                    layer.closeAll('loading');
                    if (data.code == 0) {
                        layer.msg(data.msg, {icon: 1, time: 500});
                        obj.del();
                    } else {
                        layer.msg(data.msg, {icon: 2, time: 500});
                    }
                }, 'DELETE');
            });
        };

    });
</script>